package com.student.bean;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class OperateStudent {
    private ArrayList<Student> list = new ArrayList<>();

    /**
     * 该方法添加学生信息
     */
    public void addStudent() throws IOException {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入添加学生人数");
        int number = sc.nextInt();
        String id;
        for (int i = 0; i < number; i++) {
            while (true) {
                System.out.println("请输入学生ID");
                id = sc.next();
                if (StringJudgment(id)) {
                    System.out.println("ID只能为纯数字，请重新输入");
                }
                if (findStudent(id) >= 0) {
                    System.out.println("Id相同，请重新输入");
                } else if (id.length() > 9 || id.length() < 9) {
                    System.out.println("id输入错误");
                } else {
                    break;
                }
            }
            String name;
            while (true) {
                System.out.println("输入学生姓名");
                name = sc.next();
                if (StringJudgment2(name)) {
                    System.out.println("name不能有数字");
                } else {
                    break;
                }
            }

            System.out.println("输入学生年龄");
            int age = sc.nextInt();
            Student student = new Student(id, name, age);
            list.add(student);
        }
        FileIoStudent.fileWriteLinesStudent(list);
    }

    /**
     * 该方法查询学生的信息
     * 支持单独查询和全部查询
     */
    public void inquireStudent() throws IOException {

        Scanner sc = new Scanner(System.in);
        System.out.println("查询全部请输入A，单独查询请输入O，忽略大小写");
        String str = sc.next();
        // 查询单个
        if (str.equalsIgnoreCase("A")) {

            for (Student student : list) {
                System.out.println(student.getId() + " " + student.getName() + " " + student.getAge());
            }
        }
        // 查询全部
        else if (str.equalsIgnoreCase("O")) {
            System.out.println("请输入要查询的学生ID");
            String id = sc.next();
            int index = findStudent(id);
            if (index >= 0) {
                System.out.println("该id存在,查询成功");
                for (int i = 0; i < list.size(); i++) {
                    if (index == i) {
                        System.out.println(list.get(i).getId() + " " + list.get(i).getName() + " " + list.get(i).getAge());
                    }
                }
            }
        } else {
            System.out.println("输入错误");
        }

    }


    /**
     * 该方法删除学生信息
     */
    public void deleteStudent() throws IOException {
        System.out.println("请输入要删除的学生ID");
        Scanner sc = new Scanner(System.in);
        String id = sc.next();
        int index = findStudent(id);
        if (index >= 0) {
            list.remove(index);
            System.out.println("该id存在，删除成功");
        } else {
            System.out.println("该id不存在，删除失败");
        }
        FileIoStudent.fileWriteLinesStudent(list);
    }

    /**
     * 该方法修改学生信息
     */
    public void setStudent() throws IOException {
        System.out.println("请输入要修改的学生ID");
        Scanner sc = new Scanner(System.in);
        String id = sc.next();
        int index = findStudent(id);
        if (index >= 0) {
            System.out.println("该id存在，请选择修改内容");
            System.out.println("请输入要修改的内容的首字母，忽略大小写");
            String str = sc.next();
            if (str.equalsIgnoreCase("N")) {
                System.out.println("请输入新名字");
                String name = sc.next();
                list.get(index).setName(name);
            } else if (str.equalsIgnoreCase("A")) {
                System.out.println("请输入新年龄");
                int age = sc.nextInt();
                list.get(index).setAge(age);
            } else {
                System.out.println("输入错误");
            }
        } else {
            System.out.println("该id不存在");
        }
        FileIoStudent.fileWriteLinesStudent(list);


    }

    /**
     * 查找相同ID，可以根据ID来进行查找、删除和修改。
     *
     * @return 返回ID所在位置索引
     */
    public int findStudent(String ID) {
        //遍历集合，查找相同ID
        for (int i = 0; i < list.size(); i++) {
            if (ID.equals(list.get(i).getId())) {
                return i;
            }
        }
        return -1;
    }

    public void fileIO() throws IOException {
        FileIoStudent.fileReadLinesStudent(list);
    }

    public boolean StringJudgment(String string) {
        for (int i = 0; i < string.length(); i++) {
            char ch = string.charAt(i);
            int j = (int) ch;
            if (j < 48 && j > 57) {
                return true;
            }
        }
        return false;
    }

    public boolean StringJudgment2(String string) {
        for (int i = 0; i < string.length(); i++) {
            char ch = string.charAt(i);
            int j = (int) ch;
            if (j >= 48 && j <= 57) {
                return true;
            }
        }
        return false;
    }
}
